package com.invengo.library_rfid.classify;

import android.text.TextUtils;
import android.util.Log;

import com.invengo.library_rfid.BuildConfig;
import com.invengo.library_rfid.RFIDTestUtils;
import com.invengo.library_rfid.analyze.EPCStrategy;
import com.invengo.library_rfid.entity.TagInfo;

import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.http.interceptor.logging.LocalLogUtil;
import me.goldze.mvvmhabit.utils.KLog;

public enum AnalyzeImpl implements IAnalyze {

    TO_BOOK("图书解析") {
        @Override
        public void apply(String tid, String epc, RfidModelImpl model, EPCStrategy strategy, String cacheKey) {
            if (BuildConfig.userTestData) {
                epc = RFIDTestUtils.convertEpc(epc, strategy);
            }
            String tag = "";
            try {
                synchronized (tid) {
                    if (cache.get(cacheKey).contains(model == RfidModelImpl.TID ? tid : epc)) {
                        return;
                    }
                    cache.get(cacheKey).add(model == RfidModelImpl.TID ? tid : epc);
                    switch (model) {
                        case EPC:
                        case U_DATA:
                            tag = strategy.analyzeBook(epc);
                            break;
                        case TID:
                            tag = tid;
                            break;
                    }
                    if (TextUtils.isEmpty(tag)) throw new NullPointerException("");
                    TagInfo tagInfo = new TagInfo(tag, tid, epc, model, TO_BOOK);
                    RxBus.getDefault().post(tagInfo);
                }
            } catch (Exception e) {
                if (e instanceof IllegalArgumentException) {
//                    Log.w(cacheKey, "重复数据[" + tag + "]==模式：[" + model + "]--[" + TO_BOOK + "]--[" + strategy.getRule().getRuleName() + "]");
                } else {
                    LocalLogUtil.analyze2Log(cacheKey,
                            "标签模式[ " + model + " ]" + " [ " + TO_BOOK + " ]" + " [ " + strategy.getRule().getRuleName() + " ]",
                            "原始数据[ " + tid + " ] ---- [ " + epc + " ]",
                            "解析异常[ " + e.getMessage() + " ]");
                    Log.w(cacheKey, "┌────── 解析异常 ──────────────────────────────────────────────────────────────────────── \n" +
                            "模型种类：[ " + strategy.getRule().getRuleName() + " ] \n" +
                            "标签模式：[ " + model + " ==> " + TO_BOOK + " ] \n" +
                            "异常信息：[ " + e.getMessage() + " ] \n" +
                            "原始数据：[ tid: " + tid + " -- epc: " + epc + " ] \n" +
                            "└───────────────────────────────────────────────────────────────────────────────────────");
                }
            }
        }
    }, TO_SHELF("层架解析") {
        @Override
        public void apply(String tid, String epc, RfidModelImpl model, EPCStrategy strategy, String cacheKey) {
            if (BuildConfig.userTestData)
                epc = RFIDTestUtils.convertEpc(epc, strategy);
            String tag = "";
            try {
                synchronized (tid) {
                    switch (cacheKey) {
                        case "SelectLayerFragment11":
                            if (shelfCode.equals(tid)) {
                                return;
                            }
                            shelfCode = tid;
                            break;
                        default:
                            if (cache.get(cacheKey).contains(model == RfidModelImpl.TID ? tid : epc)) {
                                return;
                            }
                            cache.get(cacheKey).add(model == RfidModelImpl.TID ? tid : epc);
                            break;
                    }
                    switch (model) {
                        case EPC:
                        case U_DATA:
                            tag = strategy.analyzeShelf(epc);
                            break;
                        case TID:
                            tag = tid;
                            break;
                    }
                    if (TextUtils.isEmpty(tag)) throw new NullPointerException("");
                    TagInfo tagInfo = new TagInfo(tag, tid, epc, model, TO_SHELF);
                    RxBus.getDefault().post(tagInfo);
                }
            } catch (Exception e) {
                if (e instanceof IllegalArgumentException) {
//                    KLog.e("===========", "标签模式[ " + model + " ]" + " [ " + TO_SHELF + " ]" + " [ " + strategy.getRule().getRuleName() + " ]" +
//                            "原始数据[ " + tid + " ] ---- [ " + epc + " ]" +
//                            "解析异常[ " + e.getMessage() + " ]");
//                    Log.w(cacheKey, "重复数据[" + tag + "]==模式：[" + model + "]--[" + TO_BOOK + "]--[" + strategy.getRule().getRuleName() + "]");
                } else {
                    LocalLogUtil.analyze2Log(cacheKey,
                            "标签模式[ " + model + " ]" + " [ " + TO_SHELF + " ]" + " [ " + strategy.getRule().getRuleName() + " ]",
                            "原始数据[ " + tid + " ] ---- [ " + epc + " ]",
                            "解析异常[ " + e.getMessage() + " ]");
                    Log.w(cacheKey, "┌────── 解析异常 ──────────────────────────────────────────────────────────────────────── \n" +
                            "模型种类：[ " + strategy.getRule().getRuleName() + " ] \n" +
                            "标签模式：[ " + model + " ==> " + TO_SHELF + " ] \n" +
                            "异常信息：[ " + e.getMessage() + " ] \n" +
                            "原始数据：[ tid: " + tid + " -- epc: " + epc + " ] \n" +
                            "└───────────────────────────────────────────────────────────────────────────────────────");
                }
            }
        }
    };
    private static String shelfCode = "";
    private String name;

    AnalyzeImpl(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


}